package com.ecoolex.discount.sales.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.YouWorldUser;
import com.ecoolex.discount.sales.pojo.AddYouWorldUserDTO;
import com.ecoolex.discount.sales.ws.YouWorldUserService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.Admin,description = "由我会员用户管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
	key = YouWorldUserGraphql.AUTH,
	name = "由我会员用户管理",
	remark = "由我会员用户管理")
public class YouWorldUserGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-YOU-WORLD-USER";
	
	public static final String AUTH_ADD = AUTH + "-ADD";
	
	public static final String AUTH_DEL = AUTH + "-DEL";
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Reference
	private YouWorldUserService youWorldUserService;
	
	@QueryField(field = "youWorldUser",description = "由我会员用户")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_GET,name = "由我会员用户")
	public YouWorldUser get(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return youWorldUserService.get(id);
	}
	
	@QueryField(field = "youWorldUserList",description = "由我会员用户列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_QUERY,name = "由我会员用户列表")
	public Pagination<YouWorldUser> query(String phone,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return youWorldUserService.query(phone, sorts, page);
	}
	
	@MutationField(field = "batchAddYouWorldUser",description = "批量添加由我会员用户")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "批量添加由我会员用户")
	public Boolean batchAdd(@NonNull List<AddYouWorldUserDTO> record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		youWorldUserService.batchAdd(record, user.userName());
		return true;
	}
	
	@MutationField(field = "delYouWorldUser",description = "删除由我会员用户")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除由我会员用户")
	public Boolean delete(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		YouWorldUser old = youWorldUserService.get(id);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "由我会员用户不存在");
		youWorldUserService.delete(old, user.userName());
		return true;
	}
}
